<?php
class ImagesController extends LilgalleryAppController {
	var $name = 'Images';
	var $uses = array('Lilgallery.Image', 'Lilgallery.Gallery');
	
	function index($gallery_id=null) {
		$this->Gallery->contain(array('Cover', 'Image'));
		$this->set('gallery', $this->Gallery->read(null, $gallery_id));
		$this->set('sidebar', 'images/index');
	}
	
	function view($image_id) {
		$this->set('sidebar', 'images/view');
		$this->set('image', $this->Image->findById($image_id));
	}
	
	function add() {
		$this->setAction('edit');
	}
	
	function edit($id = null) {
		if (!empty($this->data)) {			
			if ($this->Image->save($this->data)) {
				$this->Session->setFlash(__d('lilgallery', 'The Image has been saved', true));
				$this->redirect(array('action'=>'index', $this->data['Image']['gallery_id']));
			} else {
				$this->Session->setFlash(__d('lilgallery', 'The Image could not be saved. Please, try again.', true), 'error');
			}
		} else if (is_numeric($id)) {
  			$this->data = $this->Image->read(null, $id);
		}
		
		$this->set('galleries', $this->Image->Gallery->find('list'));
	}
	
}
?>